﻿@{      
    Layout = "../Shared/_Default.cshtml";
    ViewBag.PageTitle = T(Res.Name("Title"));
    int billingAddressID = Request.QueryParam("billingAddressID", 0);
    int shippingAddressID = Request.QueryParam("shippingAddressID", 0);
    int sellerAddressID = Request.QueryParam("sellerAddressID", 0);
    bool canBePlaced = billingAddressID != 0 &&
        shippingAddressID != 0 &&
        sellerAddressID != 0;
}

@section Toolbar
{
    @if(canBePlaced)
    {
        <a href="@Url.Action(Loc.Act("Checkout").Set(new
        {
            id = JDM.Int32(Model.Cart.EntityID)
        }).Localized())">@T(Res.In("Common").Of("Act").Name("Back"))</a>
        <span class="separator">&middot;</span>
    }
    <a href="@Url.Action(Loc.Act("Index").Ctrl("Item").Area("Store").Localized())">@T(Res.In("Common").Of("Act").Name("Cancel"))</a>
}
@if(canBePlaced)
{
    <script type="text/javascript">
        jQuery(document).ready(function () {
            jQuery('.shipping-option > section').hide();
            jQuery('.sales-model > section').hide();
            jQuery('@ID("#form") input[name=shippingMethodID]').change(function () {
                jQuery('.shipping-option > section').hide();
                jQuery('[name*="soAttributes"]').prop('readonly', true);
                jQuery('#so_' + jQuery(this).val()).show();
                jQuery('#so_' + jQuery(this).val() + ' [name*="soAttributes"]').prop('readonly', false);
            });
            jQuery('@ID("#form") input[name=salesModelID]').change(function () {
                jQuery('.sales-model > section').hide();
                if (jQuery(this).is(':checked'))
                    jQuery('#sm_' + jQuery(this).val()).show();
            });
        });
    </script>
}
<form id="@ID("form")" class="page-form"
    action="@Url.Action(canBePlaced ? Loc.Act("Place").Ctrl("Order").Localized() : Loc.Act("Checkout").Localized())" 
    method="@(canBePlaced ? "post" : "get")">
@if(canBePlaced)
{
    <input type="hidden" name="cartID" value="@JDM.Int32(Model.Cart.EntityID)" />
    <input type="hidden" name="sellerID" value="@JDM.Int32(Model.Seller.EntityID)" />
    <input type="hidden" name="billingAddressID" value="@billingAddressID" />
    <input type="hidden" name="shippingAddressID" value="@shippingAddressID" />
    <input type="hidden" name="sellerAddressID" value="@sellerAddressID" />
}
else
{
    <input type="hidden" name="id" value="@JDM.Int32(Model.Cart.EntityID)" />
}
<div class="box">
    <header>
        <nav class="inline">
            <a href="@Url.Action(Loc.Act("Index").Ctrl("Address").Area("Wizard").Localized())">@T(Res.In("WebPortal").Of("Navigation").Of("Wizard").Name("Address"))</a>
        </nav>
        <h3>@T(Res.Name("CustomerAddress"))</h3>
    </header>
    <section>
        <div class="hint">
            <p>@T(Res.Name("CustomerAddressHint"))</p>
        </div>
        @if((Model.Customer.Addresses as IEnumerable<dynamic>).Any())
        {
            <ul class="unit-grid address card customer-address">
            @foreach(var i in Model.Customer.Addresses)
            {
                <li class="unit address card customer-address">
                    <header>
                        @JDM.String(i.ContactName)
                    </header>
                    <section>
                        <span>@String.Format("{0}, {1}", JDM.String(i.Address1), JDM.String(i.Address2))</span>
                        <span>@JDM.String(i.City)</span>
                        <span>@JDM.String(i.Region.Name)</span>
                        <span>@JDM.String(i.Country.Name)</span>
                        <span>@JDM.String(i.PostalCode)</span>
                    </section>
                    <section>
                        <span>T: @JDM.String(i.PhoneNumber)</span>
                        <span>F: @JDM.String(i.FaxNumber)</span>
                        <span>@@: @JDM.String(i.Email)</span>
                    </section>
                    <section>
                        <input type="radio" name="billingAddressID" value="@JDM.Int32(i.EntityID)" 
                            @(billingAddressID == JDM.Int32(i.EntityID) ? "checked" : String.Empty)
                            @(canBePlaced ? "disabled" : String.Empty) />
                        <label>@T(Res.Name("BillingAddress"))</label>
                        <input type="radio" name="shippingAddressID" value="@JDM.Int32(i.EntityID)" 
                            @(shippingAddressID == JDM.Int32(i.EntityID) ? "checked" : String.Empty)
                            @(canBePlaced ? "disabled" : String.Empty) />
                        <label>@T(Res.Name("ShippingAddress"))</label>
                    </section>
                </li>
            }
            </ul>
        }
    </section>
</div>
<div class="box">
    <header>
        <h3>@T(Res.Name("SellerAddress"))</h3>
    </header>
    <section>
        <div class="hint">
            <p>@T(Res.Name("SellerAddressHint"))</p>
        </div>
        @if((Model.Seller.Addresses as IEnumerable<dynamic>).Any())
        {
            <ul class="unit-grid address card seller-address">
            @foreach(var i in Model.Seller.Addresses)
            {
                <li class="unit address card seller-address">
                    <header>
                        <input type="radio" name="sellerAddressID" value="@JDM.Int32(i.EntityID)"
                            @(sellerAddressID == JDM.Int32(i.EntityID) ? "checked" : String.Empty)
                            @(canBePlaced ? "disabled" : String.Empty) />
                        @JDM.String(i.ContactName)
                    </header>
                    <section>
                        <span>@JDM.String(i.EntityName)</span>
                        <span>@String.Format("{0}, {1}", JDM.String(i.Address1), JDM.String(i.Address2))</span>
                        <span>@JDM.String(i.City)</span>
                        <span>@JDM.String(i.Region.Name)</span>
                        <span>@JDM.String(i.Country.Name)</span>
                        <span>@JDM.String(i.PostalCode)</span>
                    </section>
                    <section>
                        <span>T: @JDM.String(i.PhoneNumber)</span>
                        <span>F: @JDM.String(i.FaxNumber)</span>
                        <span>W: @JDM.String(i.Website)</span>
                        <span>@@: @JDM.String(i.Email)</span>
                    </section>
                </li>
            }
            </ul>
        }
    </section>
</div>
@if((Model.ShippingOptions as IEnumerable<dynamic>).Any())
{
    <div class="box">
        <header>
            <h3>@T(Res.Name("Shipping"))</h3>
        </header>
        <section>
            <div class="hint">
                <p>@T(Res.Name("ShippingHint"))</p>
            </div>
            <ul class="unit-list shipping-option">
            @foreach(var i in Model.ShippingOptions)
            {
                <li class="unit shipping-option">
                    <header>
                        @if(JDM.Boolean(i.Availability))
                        {
                            <input type="radio" name="shippingMethodID" value="@JDM.Guid(i.Info.ID)" />
                        }
                        @String.Format("{0} [{1}]", JDM.String(i.Info.Name), JDM.Boolean(i.Availability) ? Svc.Price((decimal)JDM.Decimal(i.Price)) : "N/A")
                    </header>
                    <section id="so_@JDM.Guid(i.Info.ID)">
                    @{
                        Html.RenderPartial((string)String.Format("_so_{0}", JDM.Guid(i.Info.ID)));
                    }
                    </section>
                </li>
            }
            </ul>
        </section>
    </div>
}
@if((Model.SalesModels as IEnumerable<dynamic>).Any())
{
    <div class="box">
        <header>
            <h3>@T(Res.Name("Model"))</h3>
        </header>
        <section>
            <div class="hint">
                <p>@T(Res.Name("ModelHint"))</p>
            </div>
            <ul class="unit-list sales-model">
            @foreach(var i in Model.SalesModels)
            {
                <li class="unit sales-model">
                    <header>
                        @if(JDM.Boolean(i.Availability))
                        {
                            <input type="radio" name="salesModelID" value="@JDM.Guid(i.Info.ID)" />
                        }
                        @(JDM.String(i.Info.Name) + (JDM.Boolean(i.Availability) ? String.Empty : " [N/A]"))
                    </header>
                    <section id="sm_@JDM.Guid(i.Info.ID)">
                    @{
                        Html.RenderPartial((string)String.Format("_sm_{0}", JDM.Guid(i.Info.ID)));
                    }
                    </section>
                </li>
            }
            </ul>
        </section>
    </div>
}
@if(canBePlaced)
{
    <div class="box">
        <header>
            <h3>@T(Res.Name("Comment"))</h3>
        </header>
        <section>
            <div class="hint">
                <p>@T(Res.Name("CommentHint"))</p>
            </div>
            <textarea name="comment" maxlength="1000"></textarea>
        </section>
    </div>
}
<ul class="bar toolbar">
    <li><button class="button" type="submit">@T(Res.In("Common").Of("Act").Name("Submit"))</button></li>
</ul>
@if(canBePlaced)
{
    <table>
        <tr>
            <td>
                <label>@T(Res.Name("Coupon"))</label>
            </td>
            <td>
                <input type="hidden" name="attributes[0].Key" value="Rules.FixedDiscountRuleEntity.CouponCode" />
                <input type="text" name="attributes[0].Value" />
                <span class="hint">@T(Res.Name("CouponHint"))</span>
            </td>
        </tr>
    </table>
}
</form>
    